Completed
Push — cookie-based-auth ( 27d120...34cabe )
by Cyril
01:17
created

test-cookieAuthentication.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
cc 1
c 6
b 0
f 1
nc 1
nop 0
dl 0
loc 1
rs 10
1
const test = require('unit.js');
2
const testThat = test.promise;
3
const sinon = require('sinon');
4
const chai = require('chai');
5
const chaiAsPromised = require('chai-as-promised');
6
const expect = chai.expect;
7
const rewire = require('rewire');
8
const cookie = rewire('../lib/cookieAuthentication.js');
9
const requestify = require('requestify');
10
const getBaseUrl = cookie.__get__('getBaseUrl');
11
const extract = cookie.__get__('extractSessionCookie');
12
13
before(() => chai.use(chaiAsPromised));
14
15
beforeEach(() => sandbox = sinon.sandbox.create());
0 ignored issues
show
Bug introduced by
The variable sandbox seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.sandbox.
Loading history...
16
17
afterEach(() => sandbox.restore());
0 ignored issues
show
Bug introduced by
The variable sandbox seems to be never declared. If this is a global, consider adding a /** global: sandbox */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
18
19
20
describe.only('getBaseUrl', () => {
0 ignored issues
show
Bug introduced by
The variable describe seems to be never declared. If this is a global, consider adding a /** global: describe */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
21
  it('getBaseUrl() returns base url if valid url', () => testThat
22
    .given(() => 'http://www.dummyserver:23456/rest/api/2/issue/ID-5')
23
    .when((url) => getBaseUrl(url))
24
    .then((baseUrl) => expect(baseUrl).to.equal('http://www.dummyserver:23456/rest/'))
25
  );
26
  it('getBaseUrl() throw error if missing url', () => testThat
27
    .given()
28
    .when(() => () => getBaseUrl())
29
    .then((func) => expect(func).to.throw(Error))
30
  );
31
  it('getBaseUrl() throw error if invalid url', () => testThat
32
      .given(() => 'http://www.dummyserver:23456/bla/bla/bla')
33
      .when((url) => () => getBaseUrl(url))
34
      .then((func) => expect(func).to.throw(Error))
35
  );
36
37
  describe('extractSessionCookie', () => {
38
    it('extractSessionCookie() throw error if cookie name not specified', () => testThat
39
      .given(() => 'fake="KHG8768"')
40
      .when((cookie) => () => extract(cookie))
41
      .then((func) => expect(func).to.throw(Error))
42
    );
43
    it('extractSessionCookie() throw error if cookies not specified', () => testThat
44
      .given()
45
      .when(() => () => extract())
46
      .then((func) => expect(func).to.throw(Error))
47
    );
48
    it('extractSessionCookie() throw error if cookies is not an array', () => testThat
49
      .given(() => ({cookies: 'fake="KHG987J"', name: 'fake'}))
50
      .then((data) => () => extract(data.cookies, data.name))
51
      .then((func) => expect(func).to.throw(Error))
52
    );
53
    it('extractSessionCookie() returns sessionCookie when only one cookie', () => testThat
54
      .given(() => ({cookies: ['cookie1="LKJHLKJ8768H"'], name: 'cookie1'}))
55
      .when((data) => extract(data.cookies, data.name))
56
      .then((cookie) => expect(cookie).to.equal('cookie1="LKJHLKJ8768H"'))
57
    );
58
    it('extractSessionCookie() throw error if cookie name not found in cookies', () => testThat
59
      .given(() => ({cookies: ['cookie1="LKJHLKJ8768H"'], name: 'cookie2'}))
60
      .when((data) => () => extract(data.cookies, data.name))
61
      .then((func) => expect(func).to.throw(Error))
62
    );
63
    it('extractSessionCookie() returns sessionCookie when multiple cookies', () => testThat
64
      .given(() => ({cookies: ['cookie1="LKJHLKJ8768H"', 'cookie2="KJHG76KHJB"', 'cookie3="JRS8MLKJKJF"', 'cookie4="JH8976HGFCJ"'], name: 'cookie3'}))
65
      .when((data) => extract(data.cookies, data.name))
66
      .then((cookie) => expect(cookie).to.equal('cookie3="JRS8MLKJKJF"'))
67
    );
68
    it('extractSessionCookie() returns sessionCookie when multiple cookies and empty cookie with cookie name ignored', () => testThat
69
      .given(() => ({cookies: ['cookie1="LKJHLKJ8768H"', 'cookie2="KJHG76KHJB"', 'cookie3=""', 'cookie3="JH8976HGFCJ"'], name: 'cookie3'}))
70
      .when((data) => extract(data.cookies, data.name))
71
      .then((cookie) => expect(cookie).to.equal('cookie3="JH8976HGFCJ"'))
72
    );
73
  });
74
75
  describe('getHeader', () => {
76
    it('getHeaders() builds header including cookie', () => testThat
77
      .given(() => 'cookiename=cookievalue')
78
      .when((sessionCookie) => cookie.getHeader(sessionCookie))
79
      .then((header) => {
80
        expect(header).to.have.property('cookie', 'cookiename=cookievalue');
81
        expect(header).to.have.property('Cache-Control', 'public, max-age=60');
82
      })
83
    );
84
  });
85
86
  describe('login', () => {
87
    it('login() rejects with if url not set', () => {
88
      return expect(cookie.login('user', 'password')).to.eventually.be.rejected
89
        .then((error) => {
90
          expect(error).to.be.an.instanceof(Error);
91
        });
92
    });
93
    it('login() rejects if no cookie is in header', () => {
94
      sandbox.stub(requestify, 'post', (url, body, option) => {
0 ignored issues
show
Bug introduced by
The variable sandbox seems to be never declared. If this is a global, consider adding a /** global: sandbox */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Unused Code introduced by
The parameter option is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
95
        let headers = {'set-cookie': ''};
96
        let response = '{"session": {"name": "studio.crowd.tokenkey"}}';
97
        return Promise.resolve({code: 200, headers: headers, body: response});
98
      });
99
      expect(cookie.login('dummy-user', 'dummy-password', 'http://www.dummyurl/rest/api/2/issue/ID-78')).to.eventually.be.fulfilled;
0 ignored issues
show
introduced by
The result of the property access to expect(cookie.login("dum...eventually.be.fulfilled is not used.
Loading history...
100
    });
101
    it('login() returns session Cookie', () => {
102
      sandbox.stub(requestify, 'post', (url, body, option) => {
0 ignored issues
show
Bug introduced by
The variable sandbox seems to be never declared. If this is a global, consider adding a /** global: sandbox */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Unused Code introduced by
The parameter option is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
103
        let headers = {'set-cookie': ['atlassian.xsrf.token=BGJJ-I70H-EYI8-6QPB|2ae8e3125acff97369f184a4530b59f9d983c12d|lout; Path=/; Secure', 'JSESSIONID=913F47DAFCA6D7FF09A65537D5BD3C5C; Path=/; Secure; HttpOnly', 'studio.crowd.tokenkey=""; Domain=.ulyssjira2.atlassian.net; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; Secure; HttpOnly', 'studio.crowd.tokenkey=gW34EFQfK8Kbwpp6HkHmng00; Domain=.ulyssjira2.atlassian.net; Path=/; Secure; HttpOnly']};
104
        let response = '{"session": {"name": "studio.crowd.tokenkey"}, "loginInfo": {"failedLoginCount": 1, "loginCount": 230, "lastFailedLoginTime": "2017-01-17T10:20:43.467+0100", "previousLoginTime": "2017-01-17T17:11:46.798+0100"}}';
105
        return Promise.resolve({code: 200, headers: headers, body: response});
106
      });
107
      return expect(cookie.login('dummy-user', 'dummy-password', 'http://www.dummyurl/rest/api/2/issue/ID-78')).to.eventually.be.fulfilled
108
        .then((cookie) => {
109
          expect(cookie).to.equal('studio.crowd.tokenkey=gW34EFQfK8Kbwpp6HkHmng00');
110
        });
111
    });
112
  });
113
});
114